/**
 * <p>Title: CmesFiles.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-21 下午01:46:20  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.cInterface.cd;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.resource.I18nMessages;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.domain.ZMCDFiles;

/**
 * <p>Title: CmesFiles.java</p>
 * <p>Description: 生产文档接口</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-21 下午01:46:20  张志芳  Create
 *</pre>
 */
public class Documents extends ServiceEPBase {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");

	/**
	 * 添加文档记录，并添加附件
	 * <p>Function: insertDoc</p>
	 * <p>Description: </p>
	 * <p>return value:Boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21下午06:32:33 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Boolean insertDoc(EiInfo filesInfo,String keyId, String title, String docType, String businessType, String engineeringId){ 
		if(filesInfo.getBlock("insertfiles") != null && filesInfo.getBlock("insertfiles").getRowCount()>0)
		{
			try{			
				List<ZMCDFiles> list = getFiles(keyId);
				String tablepk = list.size()>0?list.get(0).getFdForeignId():"";
				if(list.size()<=0)
				{
					tablepk = TablePk.getInstance().getNextValue("T_CI_DOCUMENT");			
					Map params = new HashMap();
					params.put("fdKeyId", tablepk);
					params.put("fdCreatorId", UserSession.getUserId());
					params.put("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));
					params.put("fdTitle", title);
					params.put("fdCreateDate",StaticTools.getDate());
					params.put("fdTypeId", docType);
					params.put("fdBusinessId", businessType);
					params.put("fdEngineeringId", engineeringId);
					params.put("fdProfessionId", keyId);
					
					dao.insert("tZMCDDocuments.insertInterface", params);				
				}
							
				filesUpdate(filesInfo, tablepk);
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}	
	
	/**
	 * 删除对应的文档记录
	 * <p>Function: deleteDoc</p>
	 * <p>Description: </p>
	 * <p>return value:Boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21下午06:32:18 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Boolean deleteDoc(String keyId){                                                           		
		Map params = new HashMap();
		params.put("fdProfessionId", keyId);
		
		try{
			filesDelete("fdProfessionId", keyId);
			dao.delete("tZMCDDocuments.deleteInterface", params);				
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 跟据业务的ID，获得其下的所有附件的EiInfo
	 * <p>Function: getFilesInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23下午08:04:01 张志芳
	 * </p>
	 *
	 */
	public static EiInfo getFilesInfo(String blockId, String keyId)
	{
		EiInfo inInfo = new EiInfo();
		inInfo.addBlock(blockId).addBlockMeta(new ZMCDFiles().eiMetadata);
		inInfo.getBlock(blockId).addRows(getFiles(keyId));
		return inInfo;
	}	
	
	/**
	 * 跟据业务的ID，获得其下的所有附件
	 * <p>Function: getFiles</p>
	 * <p>Description: </p>
	 * <p>return value:List<ZMCDFiles></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23下午07:29:06 张志芳
	 * </p>
	 *
	 */
	public static List<ZMCDFiles> getFiles(String keyId)
	{
		return getFiles("fdProfessionId", keyId);
	}
	
	/**
	 * 可以根据文档ID和业务ID查询上传的所有文件 业务ID:keyName=fdProfessionId，文档ID:keyName=fdForeignId
	 * <p>Function: getFiles</p>
	 * <p>Description: </p>
	 * <p>return value:List<ZMCDFiles></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23下午07:40:06 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<ZMCDFiles> getFiles(String keyName, String keyId)
	{
		Map param = new HashMap();
		param.put(keyName, keyId);
		List<ZMCDFiles> _list = new ArrayList<ZMCDFiles>();
		try{
			_list = dao.query("tZMCDFiles.queryInterface", param);
		}catch(Exception e){
			
		}
		return _list;
	}

	/**
	 * 在上传服务ApacheFileUploadServlet中用
	 * <p>Function: insertFile</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24下午01:37:22 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String insertFile(String saveName, String fileName, String size)
	{
		String fdKeyId = TablePk.getInstance().getNextValue("T_CI_FILES");
		Map row = new HashMap();
		row.put("fdKeyId", fdKeyId);
		row.put("fdFileName", fileName);
		row.put("fdMemoryName", saveName);
		row.put("fdFileSize", size);
		row.put("fdForeignId", "");	
		
		try{
			dao.insert("tZMCDFiles.insert", row);
		}catch(Exception e){
			return null;
		}
		return fdKeyId;
	}
	
	/**
	 * 将附件添加在对应的文档下
	 * <p>Function: filesUpdate</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21下午06:34:10 张志芳
	 * </p>
	 *
	 */
	public static EiInfo filesUpdate(EiInfo inInfo, String tablepk) {
		
		if(inInfo.getBlock("insertfiles") != null)
		{
			for(int i=0; i<inInfo.getBlock("insertfiles").getRowCount(); i++)
			{
				inInfo.getBlock("insertfiles").setCell(i, "fdForeignId", tablepk);
				dao.update("tZMCDFiles.updateForeignId", inInfo.getBlock("insertfiles").getRow(i));
			}
		}		
		return inInfo;
	}
	
	/**
	 * 删除文件，可以根据业务ID或文档ID删除，业务ID:keyName=fdProfessionId，文档ID:keyName=fdForeignId
	 * <p>Function: filesDelete</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24上午11:33:29 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Boolean filesDelete(String keyName, String keyId) {
		
		Map params = new HashMap();
		params.put(keyName, keyId);
		String dirId = "";
		try{
			List<ZMCDFiles> list = dao.query("tZMCDFiles.queryInterface", params);
			for(int i=0; i<list.size(); i++)
			{	
				deleteLocalFile(list.get(i).getFdMemoryName());
				dirId=list.get(i).getFdForeignId();
			}
			if(!dirId.equals(""))
			{
				params.clear();
				params.put("fdKeyId", dirId);
				dao.delete("tZMCDFiles.delete", params);
			}
			
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 根据文件名找到文件，并删除
	 * <p>Function: deleteLocalFile</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24下午12:59:49 张志芳
	 * </p>
	 *
	 */
	public static void deleteLocalFile(String fileName) throws Exception{
		try {
			String keepDir = I18nMessages.getText("sczx.updownload.uploadfilekeepeddir");
			String path = keepDir + fileName;
			File file = new File(path);
	
			if(file.exists()){
			System.gc();
			file.delete();
		}

		} catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 判断某一文档目录下是否有文档记录
	 * <p>Function: isExistDoc</p>
	 * <p>Description: </p>
	 * <p>return value:Boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-1下午12:51:36 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Boolean isExistDoc(String dirId)
	{		
		Map params = new HashMap();
		params.put("fdDirId", dirId);	
		try{
			List list = dao.query("tZMCDDocuments.query", params);		
			if(list.size() > 0)
			{
				return true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
